package com.tyylab.message.distribute.message;

import com.alibaba.fastjson.JSON;
import com.tyylab.message.core.model.TransMessage;
import com.tyylab.message.core.service.IPublish;
import com.tyylab.message.core.service.IUniqueSupport;
import com.tyylab.message.distribute.distribute.IChannelGetter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
@Slf4j
public class SimulEventRepublish {
    @Autowired
    IPublish publishSevice;

    @Autowired
    IChannelGetter channelGetter;

    @Autowired
    IUniqueSupport uniqueSupport;
    static final String IS_UNIQUE_KEY_NAME = "isUnique";
    static final String HASH_KEY_NAME = "hash";


    public void republish(TransMessage message){
        String clzName = message.getClz();
        if (StringUtils.isBlank(clzName)){
            log.error("Message 错误，缺少Clz：" + JSON.toJSONString(message));
            return;
        }
        Collection<String> channels = channelGetter.getTargetChannel(clzName);
        if (null != channels && !channels.isEmpty()) {
            for (String channel :
                    channels) {
                boolean supportUnique = false;
                String hashkey = "";
                String msgId = "" ;
                if (channelGetter.isSupportUnique(channel)){

                    int isUnque = message.getMessage().containsKey(IS_UNIQUE_KEY_NAME)?message.getMessage().getIntValue(IS_UNIQUE_KEY_NAME) : 0;
                    if (isUnque>0 && message.getMessage().containsKey(HASH_KEY_NAME)){
                        hashkey = message.getMessage().getString(HASH_KEY_NAME);
                        if (StringUtils.isNotBlank(hashkey)) {
                            supportUnique = true;
                            msgId = message.getMessage().getString("id");
                        }
                    }

                }
                if (supportUnique){
                    hashkey = hashkey +":"+ message.getClz();
                    String existId = uniqueSupport.getExistMsgId(channel,hashkey);
                    if (StringUtils.isBlank(existId)) {
                        log.info("write hash key =>" + hashkey);
                        uniqueSupport.addUniqueKey(channel, hashkey, msgId);
                        log.info("republish to channel (support unique):" + channel);
                        Integer oVal = message.getIsSupportUnique();
                        message.setIsSupportUnique(1);
                        log.info("republish to channel (support unique):" + channel + "," + JSON.toJSONString(message));
                        publishSevice.publish(message, channel);
                        message.setIsSupportUnique(oVal);
                    }
                    else {
                        log.info("exist unique event ,id is " + existId + ", channel is " + channel + ", drop => " + JSON.toJSONString(message));
                    }
                }
                else {
                    log.info("republish to channel (Not support unique):" + channel);
                    publishSevice.publish(message, channel);
                }
            }
        }
        else{
            log.warn("Not config distribute channel, republish to channel: nodistribute");
            publishSevice.publish(message, "nodistribute");
        }
    }
}
